package com.linkage.educloud.ah.mvp.presenter.topic;

import android.content.Context;

import com.linkage.educloud.ah.bean.TopicChildBean;
import com.linkage.educloud.ah.bean.TopicGroupBean;
import com.linkage.educloud.ah.http.Method;
import com.linkage.educloud.ah.http.RequestUtil;
import com.linkage.educloud.ah.mvp.contract.topic.ITopicMineContract;
import com.linkage.educloud.ah.mvp.model.topic.TopicMineModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.BaseSubscriber;
import com.zzhou.common.library.util.ToastShow;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observable;

/**
 * Created by Mr'Tang on 2018/12/7.
 */

public class TopicMinePresenter extends ITopicMineContract.IPresenter<ITopicMineContract.IView> {
    Context mContext;
    ITopicMineContract.IModel iModel;

    public TopicMinePresenter(Context context, ITopicMineContract.IView iView) {
        super(iView);
        mContext = context;
        iModel = new TopicMineModel();
    }

    //获取我回复的帖子
    @Override
    public void getMyReplyCards(int page) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.getMyReplyCards(page));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        ((ITopicMineContract.IView) iView).stopRefresh();
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 0) {
                                ((ITopicMineContract.IView) iView).initRecylerviewDate(TopicChildBean.parseFromJson3(json.optJSONArray("data")));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }

    //获取我发布的帖子
    @Override
    public void getMyCards(int page) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.getMyCards(page));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        ((ITopicMineContract.IView) iView).stopRefresh();
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 0) {
                                ((ITopicMineContract.IView) iView).initRecylerviewDate(TopicChildBean.parseFromJson3(json.optJSONArray("data")));
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }
}
